package com.vipheyue.fastlib.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * RecyclerView的万能适配器
 */
public abstract class MultiTypeAdpter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {

    /**
     * 布局加载器
     */
    private LayoutInflater mInflater;


    public MultiTypeAdpter(Context mContext) {
        super();
        mInflater = LayoutInflater.from(mContext);
    }

    /**
     * 创建ViewHolder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        /**
         * 加载item布局，生成ViewHolder并返回
         */
        View itemView = mInflater.inflate(holderLayoutIdOfType(viewType), parent, false);//TODO
        BaseRecyclerViewHolder holder = new BaseRecyclerViewHolder(itemView);
        return holder;
    }

    public abstract int holderLayoutIdOfType(int viewType);//由使用者自己确定 switch

    /**
     * 绑定ViewHolder
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, int position) {
        init(holder, getItemViewType(position), position);
        /**
         * item单击事件监听
         */
        if (onItemClickLinster != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickLinster.onItemClick(
                            holder.itemView, holder.getLayoutPosition());//或者 position 有添加删除最好用 getLayoutPosition
                }
            });
        }

        /**
         * item长按事件监听
         */
        if (onItemLongClickLinster != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemLongClickLinster.onItemLongClick(
                            holder.itemView, holder.getLayoutPosition());
                    return true;
                }
            });
        }
    }

    /**
     * item的数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return itemCount();
    }

    public abstract int itemCount();//由使用者自己确定

    @Override
    public int getItemViewType(int position) {
        //TODO 根据position 来确定Type
        return itemViewType(position);
    }

    public abstract int itemViewType(int position);//由使用者自己确定


    /**
     * 重写此方法，在里面初始化item控件和设置item控件中的值
     *
     * @param holder
     * @param itemViewType 即 itemViewType
     */
    public abstract void init(BaseRecyclerViewHolder holder, int itemViewType, int position);

    /**
     * 单击事件监听的回调接口
     */
    public interface OnItemClickLinster {
        void onItemClick(View view, int position);
    }

    private OnItemClickLinster onItemClickLinster;

    public void setOnItemClickLinster(OnItemClickLinster onItemClickLinster) {
        this.onItemClickLinster = onItemClickLinster;
    }

    /**
     * 长按事件监听的回调接口
     */
    public interface OnItemLongClickLinster {
        void onItemLongClick(View view, int position);
    }

    private OnItemLongClickLinster onItemLongClickLinster;

    public void setOnItemLongClickLinster(OnItemLongClickLinster onItemLongClickLinster) {
        this.onItemLongClickLinster = onItemLongClickLinster;
    }


}